-----------------------------------------------------------
--ECE 251 Switch - MII block - by Peter Gebhard--
-----------------------------------------------------------

LIBRARY ieee;
USE ieee.std_logic_1164.all;

ENTITY mii IS
   PORT(clk          : IN   STD_LOGIC;
        data         : IN   STD_LOGIC_VECTOR(3 DOWNTO 0);
        data_valid   : IN   STD_LOGIC;
        buffer_ready : IN   STD_LOGIC;
        buffer_req   : OUT  STD_LOGIC;
        q            : OUT  STD_LOGIC_VECTOR(15 DOWNTO 0)
        );
END mii;

-- Start shifting out data after request signal gets a ready signal back

ARCHITECTURE Behavior OF mii IS

BEGIN
   PROCESS (clk)
   BEGIN
      IF (clk'EVENT AND clk = '1') THEN
         
         -- Handle dropping of frames --
         IF (drop_frame='1') THEN
            mii_ready <= 0;
            WHILE d != <END OF FRAME PATTERN> LOOP
            END LOOP;
         END IF;
         
         -- TODO: add in functionality to be able to parse the length..
         -- this parsing would make frame drops easier.
         IF (d_valid='1') THEN
            mii_ready <= 0;
            q(N*4-1 DOWNTO N*4-4) <= d(3 DOWNTO 0);
            N <= N-1;
         
            -- Done with 16bit repackaging of incoming data
            -- TODO: add in temporary registers to allow the controller some
            -- time to get the data.
            IF (N='0') THEN
               mii_ready <= 1;
               N <= 4;
            END IF;
         END IF;
      END IF;
   END PROCESS;
END Behavior;